package org.example.service;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.HashMap;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.mapper.JobtransferMapper;
import org.example.entity.Jobtransfer;
import org.example.entity.Department;
import org.example.entity.Post;
import org.springframework.beans.factory.annotation.Autowired;

@Service
public class JobtransferService extends ServiceImpl<JobtransferMapper, Jobtransfer> {

    @Autowired
    private DepartmentService departmentService;

    @Autowired
    private PostService postService;

    public Page<Jobtransfer> getPageWithNames(Page<Jobtransfer> page) {
        // 先查询基本数据
        Page<Jobtransfer> pageResult = this.page(page);

        // 获取所有部门和岗位数据
        List<Department> departments = departmentService.list();
        List<Post> posts = postService.list();

        // 创建映射表提高查找效率
        Map<Integer, String> deptMap = new HashMap<>();
        Map<Integer, String> postMap = new HashMap<>();

        for (Department dept : departments) {
            deptMap.put(dept.getDeptId(), dept.getDeptName());
        }

        for (Post post : posts) {
            postMap.put(post.getPostId(), post.getPostName());
        }

        // 为每条记录设置部门和岗位名称
        for (Jobtransfer jobtransfer : pageResult.getRecords()) {
            if (jobtransfer.getTransferDepartment() != null) {
                jobtransfer.setTransferDepartmentName(
                        deptMap.getOrDefault(jobtransfer.getTransferDepartment(), "未知部门")
                );
            } else {
                jobtransfer.setTransferDepartmentName("未指定");
            }

            if (jobtransfer.getTransferPosition() != null) {
                jobtransfer.setTransferPositionName(
                        postMap.getOrDefault(jobtransfer.getTransferPosition(), "未知岗位")
                );
            } else {
                jobtransfer.setTransferPositionName("未指定");
            }
        }

        return pageResult;
    }
}